package com.shihb.akka.yellochicken.server

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import com.shihb.akka.yellochicken.common.{ClientMessage, ServerMessage}
import com.typesafe.config.{Config, ConfigFactory}

/**
 * Description:小黄鸡服务端
 *
 * @author shihb
 * @date 2020/10/2 21:43
 */
class YellowChickServer extends Actor {
  override def receive: Receive = {
    case "start" => println("小黄鸡客服开始工作了...")
    case ClientMessage(msg) => {
      // 使用match --case 匹配
      msg match {
        case "大数据学费" => sender ! ServerMessage("35000RMB")
        case "学校地址" => sender ! ServerMessage("红星大都会")
        case "学习什么技术" => sender ! ServerMessage("大数据 前端 python")
        case _ => sender ! ServerMessage("你说啥子")
      }

    }
  }
}

/**
 * 主程序入口
 */
object YellowChickServer extends App {
  //创建ActorSystem
  // url统一资源定位

  val host = "127.0.0.1"
  val port = 9999
  val config: Config = ConfigFactory.parseString(
    s"""
       |akka.actor.provider="akka.remote.RemoteActorRefProvider"
       |akka.remote.netty.tcp.hostname=$host
       |akka.remote.netty.tcp.port=$port
       |""".stripMargin)
  val actorFactory: ActorSystem = ActorSystem("server", config)
  //创建Actor
  val yellowChickServerRef: ActorRef = actorFactory.actorOf(Props[YellowChickServer], "YellowChickServer")
  // 启动
  yellowChickServerRef ! "start"

}
